﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using GMLogging;

namespace GameControls
{
    public sealed class Label : ControlBase
    {
        #region Properties
        #region HorizontalAlignment
        /// <summary>
        /// HorizontalAlignment property.
        /// </summary>
        public HorizontalAlignment HorizontalAlignment { get; set; }
        #endregion

        #region VerticalAlignment
        /// <summary>
        /// VerticalAlignment property.
        /// </summary>
        public VerticalAlignment VerticalAlignment { get; set; }
        #endregion

        #region Text
        /// <summary>
        /// Text property.
        /// </summary>
        public string Text { get; set; }
        #endregion

        #region Font
        /// <summary>
        /// Font property.
        /// </summary>
        public SpriteFont Font { get; set; }
        #endregion

        #region TextColor
        /// <summary>
        /// TextColor property.
        /// </summary>
        public Color TextColor { get; set; }
        #endregion
        #endregion

        #region Events
        #endregion

        #region Constructors
        public Label(Rectangle pRect)
            : base(pRect)
        {
            ClearText();

            HorizontalAlignment = HorizontalAlignment.Center;
            VerticalAlignment = VerticalAlignment.Center;
        }
        #endregion

        #region Overriden Methods
        protected override void UpdateImpl(GameTime pGameTime)
        {
        }
        #endregion

        #region Methods
        public void SetText(string pText, SpriteFont pFont, Color pTextColor)
        {
            Text = pText;
            Font = pFont;
            TextColor = pTextColor;
        }

        public void ClearText()
        {
            Text = null;
            Font = null;
            TextColor = Color.Black;
        }

        public bool IsTextValid()
        {
            return !string.IsNullOrEmpty(Text) && Font != null;
        }

        public Vector2 GetTextPosition()
        {
            if (IsTextValid())
            {
                Rectangle transformedRect = TransformedRect;
                Vector2 textSize = Font.MeasureString(Text);

                float textPositionX;
                switch (HorizontalAlignment)
                {
                    case HorizontalAlignment.Left:
                        textPositionX = transformedRect.X;
                        break;

                    case HorizontalAlignment.Center:
                        textPositionX = transformedRect.X + transformedRect.Width / 2 - textSize.X / 2;
                        break;

                    case HorizontalAlignment.Right:
                        textPositionX = transformedRect.X + transformedRect.Width - textSize.X;
                        break;

                    default:
                        throw new NotImplementedException(string.Format("HorizontalAlignment not implemented: {0}", HorizontalAlignment));
                }

                float textPositionY;
                switch (VerticalAlignment)
                {
                    case VerticalAlignment.Top:
                        textPositionY = transformedRect.Y;
                        break;

                    case VerticalAlignment.Center:
                        textPositionY = transformedRect.Y + transformedRect.Height / 2 - textSize.Y / 2;
                        break;

                    case VerticalAlignment.Bottom:
                        textPositionY = transformedRect.Y + transformedRect.Height - textSize.Y;
                        break;

                    default:
                        throw new NotImplementedException(string.Format("VerticalAlignment not implemented: {0}", VerticalAlignment));
                }

                return new Vector2(textPositionX, textPositionY);
            }
            else
            {
                return Vector2.Zero;
            }
        }
        #endregion
    }
}